<?php

/**
 *
 * @copyright   Copyright(c) 2011
 * @author      yuansir <yuansir@live.cn/yuansir-web.com>
 * @version     1.0
 */
class homeModel extends Model {
	/*
	 * 列表展现
	 */
	function showlist($spage, $keyword = null,$classid=null) {
		$db = $this->db;
		$pageNum = 20;
		$page = $this->load("page");
		$start = ($spage -1) * $pageNum;
		$end = $start + $pageNum;
		$where = " where 1 ";
		if ($keyword)
			$where .= " and title like '%" . $keyword . "%'";
		if ($classid)
			$where .= " and classid=" . $classid;
		$sql = "select a.*,ac.className from articles as a left join article_class as ac on a.classid=ac.id" . $where . " limit $start,$pageNum";
		$data = $db->getAll($sql);
		$sql = "select count(*) from articles" . $where;
		$count = $db->getOne($sql);
		$page->Paging(array (
			'total' => $count,
			'perpage' => $pageNum
		));
		return $result = array (
			'data' => $data,
			'page' => $page->show(2)
			);
	}
	/*
	 * 查看页面
	 */
	function showpage($id) {
		$db = $this->db;
		$sql = "select * from articles where id=" . $id;
		$data = $db->getRow($sql);
		$data['content'] = htmlspecialchars_decode($data['content']);
		return $result = array (
			'data' => $data
		);
	}
	/*
	 * 获取系统类别名称
	 */
	public function showClass() {
		$sql = "select * from article_class where articleCount>0";
		$data = $this->db->getAll($sql,'id');
		return $data;
	}
	/*
	 * 更新分类下的文章数量
	 */
	 public function updateClass()
	 {
	 	$sql = "select ac.id,count(*) as sum from articles as a left join article_class as ac on ac.id=a.classid GROUP BY a.classid";
		$data = $this->db->getAll($sql,'id');
		foreach($data as $id=>$count)
		{
			$upd = array('articleCount'=>$count['sum']);
			$where = " id= ".$id;
			$this->db->update("article_class",$upd,$where);
		}
	 }
}



